<?php
class Model extends Model {
	
	var $config_safe_delete = 0;
	
	public function add($data) {
      	$this->db->query("INSERT INTO `" . DB_PREFIX . "` SET " .
      							" id = '" . $this->db->escape($data['id']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .      			 
				"'");

      	$id = $this->db->getLastId();
      	
      	return $id;
	}
	
	public function edit($id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "` SET " .
								" id = '" . $this->db->escape($data['id']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .				"', name = '" . $this->db->escape($data['name']) .				 
				"' WHERE `id` = '" . (int)$id . "'");		
	}
	
	public function delete($id) {
		if($this->config_safe_delete)
			$this->db->query("UPDATE `" . DB_PREFIX . "` SET  status WHERE `id` = '" . (int)$id . "'");
		else
      		$this->db->query("DELETE FROM `" . DB_PREFIX . "` WHERE `id` = '" . (int)$id . "'");
	}
	
	public function get($id) {
		
      	$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "` WHERE `id` = '" . (int)$id . "'");
		
		return $query->row;
	}
	
	public function gets($data = array()) {
		if($this->config_safe_delete)
			$sql = "SELECT `id`, `id`,`name`,`name`,`name`,`name` FROM `" . DB_PREFIX . "` WHERE 1=1  ";
		else
			$sql = "SELECT `id`, `id`,`name`,`name`,`name`,`name` FROM `" . DB_PREFIX . "` WHERE 1=1 ";
		
				if (isset($data['filter_id']) && !is_null($data['filter_id'])) {
			$sql .= " AND id LIKE '%" . $this->db->escape($data['filter_id']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}


		
		$sort_data = array(
						'id',			'name',			'name',			'name',			'name',
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY `id`";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotals() {
		if($this->config_safe_delete)
			$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "` WHERE 1=1  ";
		else
			$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "` WHERE 1=1 ";
		
				if (isset($data['filter_id']) && !is_null($data['filter_id'])) {
			$sql .= " AND id LIKE '%" . $this->db->escape($data['filter_id']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}


		
      	$query = $this->db->query($sql);
      	
		return $query->row['total'];
	}
	
				
}
?>